<template>
  <div class="register-container">
    <el-form ref="registerForm" :model="registerForm" :rules="registerRules" class="register-form" auto-complete="on" label-position="left">

      <div class="title-container">
        <h3 class="title" style="color: black; font-weight: bold">输入法后台管理系统</h3>
      </div>

      <el-form-item prop="username" style="width: 300px; border-radius: 50px; padding: 0 5px; background-color: rgba(0,0,0,0);">
        <span class="svg-container">
          <svg-icon icon-class="user" />
        </span>
        <el-input
          ref="username"
          v-model="registerForm.username"
          placeholder="请输入帐号"
          name="username"
          type="text"
          tabindex="1"
          auto-complete="on"
        />
      </el-form-item>

      <el-form-item prop="invite" style="width: 300px; border-radius: 50px; padding: 0 5px; background-color: rgba(0,0,0,0);">
        <span class="svg-container">
          <svg-icon icon-class="eye-open" />
        </span>
        <el-input
          ref="invite"
          v-model="registerForm.invite"
          placeholder="请输入邀请码"
          name="invite"
          type="text"
          tabindex="2"
          auto-complete="on"
          />
        </el-form-item>

      <el-form-item prop="password" style="width: 300px; border-radius: 50px; padding: 0 5px; background-color: rgba(0,0,0,0);">
        <span class="svg-container">
          <svg-icon icon-class="password" />
        </span>
        <el-input
          :key="passwordType"
          ref="password"
          v-model="registerForm.password"
          :type="passwordType"
          placeholder="请输入注册密码"
          name="password"
          tabindex="3"
          auto-complete="on"
          @keyup.enter.native="handleregister"
        />
        <span class="show-pwd" @click="showPwd">
          <svg-icon :icon-class="passwordType === 'password' ? 'eye' : 'eye-open'" />
        </span>
      </el-form-item>

      <el-form-item prop="confirmpwd" style="width: 300px; border-radius: 50px; padding: 0 5px; background-color: rgba(0,0,0,0);">
        <span class="svg-container">
          <svg-icon icon-class="password" />
        </span>
        <el-input
          :key="passwordType"
          ref="password"
          v-model="registerForm.confirmpwd"
          :type="passwordType"
          placeholder="请输入确认密码"
          name="confirmpwd"
          tabindex="4"
          auto-complete="on"
          @keyup.enter.native="handleregister"
        />
        <span class="show-pwd" @click="showPwd">
          <svg-icon :icon-class="passwordType === 'password' ? 'eye' : 'eye-open'" />
        </span>
      </el-form-item>

      <el-button class="myButton" :loading="loading" type="primary" @click.native.prevent="handleregister">注册</el-button>
      <el-button class="myButton" :loading="loading" type="primary" @click="$router.push('login')">已有帐号，进行登录</el-button>
    </el-form>
  </div>
</template>

<script>
import { validUsername } from '@/utils/validate'

export default {
  name: 'register',
  data() {
    const validateUsername = (rule, value, callback) => {
      // if (!validUsername(value)) {
      //   callback(new Error('请输入正确的帐号'))
      // } else {
      //   callback()
      // }
    }
    const validatePassword = (rule, value, callback) => {
      if (value.length < 6) {
        callback(new Error('密码不得少于6位'))
      } else {
        callback()
      }
    }
    const validateInvite = (rule, value, callback) => {
      // if (!validateInvite(value)) {
      //   callback(new Error('请输入正确的邀请码'))
      // } else {
      //   callback()
      // }
    }
    const validateConfirmPwd = (rule, value, callback) => {
      if (value === "") {
        callback(new Error("请再次输入密码"));
      } else if (value !== this.registerForm.password) {
        callback(new Error("两次输入密码不一致!"));
      } else {
        callback();
      }

    }
    return {
      registerForm: {
        // username: 'admin',
        // invite: '1'||'2',
        // password: '111111',
        // confirmpwd: '111111'
      },
      registerRules: {
        username: [{ required: true, trigger: 'blur'}],
        password: [{ required: true, trigger: 'blur', validator: validatePassword }],
        invite: [{ required: true}],
        confirmpwd: [{ required: true, trigger: 'blur', validator: validateConfirmPwd }]
      },
      loading: false,
      passwordType: 'password',
      redirect: undefined
    }
  },
  watch: {
    $route: {
      handler: function(route) {
        this.redirect = route.query && route.query.redirect
      },
      immediate: true
    }
  },
  methods: {
    showPwd() {
      if (this.passwordType === 'password') {
        this.passwordType = ''
      } else {
        this.passwordType = 'password'
      }
      this.$nextTick(() => {
        this.$refs.password.focus()
      })
    },
    handleregister() {
      
      this.$refs.registerForm.validate(valid => {
        if (valid) {
          this.loading = true
          this.$store.dispatch('user/register', this.registerForm).then(() => {
            this.$router.push({ path: this.redirect || '/login' })
            this.loading = false
          }).catch(() => {
            this.loading = false
          })
        } else {
          console.log('error submit!!')
          return false
        }
      })
    }
  }
}

</script>

<style lang="scss">
/* 修复input 背景不协调 和光标变色 */
/* Detail see https://github.com/PanJiaChen/vue-element-admin/pull/927 */

$bg:#283443;
$dark_gray:#889aa4;
$light_gray:#fff;
$cursor: $dark_gray;

@supports (-webkit-mask: none) and (not (cater-color: $cursor)) {
  .register-container .el-input input {
    color: $cursor;
  }
}

/* reset element-ui css */
.register-container {
  .el-input {
    display: inline-block;
    height: 47px;
    width: 85%;

    input {
      background: transparent;
      border: 0px;
      -webkit-appearance: none;
      border-radius: 0px;
      padding: 12px 5px 12px 15px;
      color: black;//修改输入框中文字的颜色
      height: 47px;
      caret-color: $cursor;

      &:-webkit-autofill {
        box-shadow: 0 0 0px 1000px $bg inset !important;
        -webkit-text-fill-color: $cursor !important;
      }
    }
  }

  .el-form-item {
    border: 1px solid rgba(255, 255, 255, 0.1);
    background: rgba(0, 0, 0, 0.1);
    border-radius: 5px;
    //color: black;
  }
}

</style>

<style lang="scss" scoped>
$bg:#2d3a4b;
$dark_gray:#889aa4;
$light_gray:#eee;

.register-container {
  min-height: 100%;
  width: 100%;
  overflow: hidden;
  background-image: url("../../assets/loginBG.png");
  background-size: auto 100%;
  background-position: center;

  display: flex;
  align-items: center;

  .register-form {
    position: relative;
    width: auto;
    max-width: 100%;
    padding: 50px 50px;
    margin: auto 80px auto auto;
    overflow: hidden;

    //background-color: ;
    background-color: rgba(#F5F5FF,0.85);
    //opacity: 0;
    border-radius: 15px;

    display: flex;
    flex-direction: column;
    align-items: center;
    .myButton {
      border-radius: 50px;
      width: 60%;
      margin-left: 0px;
      margin-bottom: 15px;
    }
  }

  .tips {
    font-size: 14px;
    color: #fff;
    margin-bottom: 10px;

    span {
      &:first-of-type {
        margin-right: 16px;
      }
    }
  }

  .svg-container {
    padding: 6px 5px 6px 15px;
    color: $dark_gray;
    vertical-align: middle;
    width: 30px;
    display: inline-block;
  }

  .title-container {
    position: relative;

    .title {
      font-size: 26px;
      color: $light_gray;
      margin: 0px auto 40px auto;
      text-align: center;
      font-weight: bold;
    }
  }

  .show-pwd {
    position: absolute;
    right: 10px;
    top: 7px;
    font-size: 16px;
    color: $dark_gray;
    cursor: pointer;
    user-select: none;
  }
}
</style>
